﻿using System;
using System.Collections.Generic;
using System.IO;

namespace SharpSqlAnalyzer.scriptWriter
{
	// Token: 0x020001F0 RID: 496
	public class TScriptWriter
	{
		// Token: 0x0600112A RID: 4394 RVA: 0x00313888 File Offset: 0x00311A88
		public virtual void addComparisonOperator(EComparisonType comparisonType)
		{
			this.addSpace(1);
			switch (comparisonType)
			{
			case EComparisonType.equalsTo:
				this.addSymbol("=");
				break;
			case EComparisonType.greaterThan:
				this.addSymbol(">");
				break;
			case EComparisonType.lessThan:
				this.addSymbol("<");
				break;
			case EComparisonType.greaterThanOrEqualTo:
				this.addSymbol(">=");
				break;
			case EComparisonType.lessThanOrEqualTo:
				this.addSymbol("<=");
				break;
			case EComparisonType.notEqualToBrackets:
				this.addSymbol("<>");
				break;
			case EComparisonType.notEqualToExclamation:
				this.addSymbol("!=");
				break;
			case EComparisonType.notEqualToCaret:
				this.addSymbol("^=");
				break;
			case EComparisonType.notLessThan:
				this.addSymbol("!<");
				break;
			case EComparisonType.notLessThanToCaret:
				this.addSymbol("^<");
				break;
			case EComparisonType.notGreaterThan:
				this.addSymbol("!>");
				break;
			case EComparisonType.notGreaterThanToCaret:
				this.addSymbol("^>");
				break;
			}
			this.addSpace(1);
		}

		// Token: 0x0600112B RID: 4395 RVA: 0x00313984 File Offset: 0x00311B84
		public virtual void addUnaryOperator(EExpressionType expressionType)
		{
			switch (expressionType)
			{
			case EExpressionType.unary_plus_t:
				this.addSymbol("+");
				return;
			case EExpressionType.unary_minus_t:
				this.addSymbol("-");
				return;
			case EExpressionType.unary_prior_t:
				this.addKeyword("prior");
				return;
			case EExpressionType.unary_connect_by_root_t:
				this.addKeyword("connect_by_root");
				return;
			default:
				if (expressionType != EExpressionType.unary_bitwise_not_t)
				{
					return;
				}
				this.addSymbol("~");
				return;
			}
		}

		// Token: 0x0600112C RID: 4396 RVA: 0x003139EC File Offset: 0x00311BEC
		public virtual void addBinaryOperator(EExpressionType expressionType)
		{
			if (expressionType == EExpressionType.collate_t)
			{
				this.addKeyword("COLLATE");
				return;
			}
			this.addSpace(1);
			if (expressionType <= EExpressionType.logical_or_t)
			{
				switch (expressionType)
				{
				case EExpressionType.arithmetic_plus_t:
					this.addSymbol("+");
					break;
				case EExpressionType.arithmetic_minus_t:
					this.addSymbol("-");
					break;
				case EExpressionType.arithmetic_times_t:
					this.addSymbol("*");
					break;
				case EExpressionType.arithmetic_divide_t:
					this.addSymbol("/");
					break;
				case EExpressionType.arithmetic_modulo_t:
					this.addSymbol("%");
					break;
				case EExpressionType.arithmetic_compound_operator_t:
				case EExpressionType.arithmetic_exponentiation_t:
				case EExpressionType.parenthesis_t:
				case EExpressionType.assignment_t:
				case EExpressionType.list_t:
				case EExpressionType.bitwise_t:
					break;
				case EExpressionType.concatenate_t:
					this.addSymbol("||");
					break;
				case EExpressionType.bitwise_and_t:
					this.addSymbol("&");
					break;
				case EExpressionType.bitwise_or_t:
					this.addSymbol("|");
					break;
				case EExpressionType.bitwise_xor_t:
					this.addSymbol("#");
					break;
				case EExpressionType.bitwise_exclusive_or_t:
					this.addSymbol("^");
					break;
				default:
					if (expressionType != EExpressionType.logical_and_t)
					{
						if (expressionType == EExpressionType.logical_or_t)
						{
							this.addKeyword("or");
						}
					}
					else
					{
						this.addKeyword("and");
					}
					break;
				}
			}
			else if (expressionType != EExpressionType.left_join_t)
			{
				if (expressionType != EExpressionType.right_join_t)
				{
					if (expressionType == EExpressionType.sqlserver_proprietary_column_alias_t)
					{
						this.addSymbol("=");
					}
				}
				else
				{
					this.addSymbol("=*");
				}
			}
			else
			{
				this.addSymbol("*=");
			}
			this.addSpace(1);
		}

		// Token: 0x0600112D RID: 4397 RVA: 0x00313B67 File Offset: 0x00311D67
		public virtual void reset()
		{
			this.tokenList.Clear();
		}

		// Token: 0x0600112E RID: 4398 RVA: 0x00313B74 File Offset: 0x00311D74
		public virtual void writeTo(TextWriter writer)
		{
			foreach (TSourceToken tsourceToken in this.tokenList)
			{
				try
				{
					writer.Write(tsourceToken.ToString());
				}
				catch (IOException ex)
				{
					Console.WriteLine(ex.ToString());
					Console.Write(ex.StackTrace);
				}
			}
		}

		// Token: 0x0600112F RID: 4399 RVA: 0x00313BEC File Offset: 0x00311DEC
		public virtual bool verifyTokens(TSourceTokenList originalTokens, bool partialChecking)
		{
			bool flag = true;
			int num = 0;
			bool flag2 = false;
			int num2 = 0;
			for (int i = 0; i < this.tokenList.Count; i++)
			{
				if (this.tokenList[i].tokentype == ETokenType.ttkeyword && i != this.tokenList.Count - 1)
				{
					if (this.tokenList[i + 1].tokencode != 260 && this.tokenList[i + 1].tokencode != 259 && this.tokenList[i + 1].tokencode != 40 && this.tokenList[i + 1].tokencode != 41)
					{
						Console.Write("lack space after keyword:" + this.tokenList[i].ToString());
						flag = false;
						break;
					}
				}
				else if (this.tokenList[i].tokentype == ETokenType.ttidentifier && i != 0 && (this.tokenList[i - 1].tokentype == ETokenType.ttkeyword || this.tokenList[i - 1].tokentype == ETokenType.ttidentifier))
				{
					Console.Write("lack space between identifier and keyword:" + this.tokenList[i].ToString());
					flag = false;
					break;
				}
			}
			if (!flag)
			{
				return flag;
			}
			for (int j = 0; j < originalTokens.size(); j++)
			{
				if (originalTokens.get(j).tokencode != 260 && originalTokens.get(j).tokencode != 259 && originalTokens.get(j).tokentype != ETokenType.ttsimplecomment && originalTokens.get(j).tokentype != ETokenType.ttbracketedcomment && originalTokens.get(j).tokentype != ETokenType.ttsemicolon)
				{
					if (partialChecking)
					{
						if (originalTokens.get(j).tokencode == 40)
						{
							flag2 = true;
							num2++;
						}
						else if (originalTokens.get(j).tokencode == 41)
						{
							if (num2 > 0)
							{
								num2--;
							}
							if (num2 == 0 && flag2)
							{
								flag = true;
								break;
							}
						}
					}
					flag = false;
					for (int k = num; k < this.tokenList.Count; k++)
					{
						if (this.tokenList[k].tokencode != 260 && this.tokenList[k].tokencode != 259 && this.tokenList[k].tokentype != ETokenType.ttsimplecomment && this.tokenList[k].tokentype != ETokenType.ttbracketedcomment && this.tokenList[k].tokentype != ETokenType.ttsemicolon)
						{
							flag = ((originalTokens.get(j).tokencode == 289 && this.tokenList[k].tokencode == 289) || originalTokens.get(j).ToString().Equals(this.tokenList[k].ToString(), StringComparison.CurrentCultureIgnoreCase));
							num = k + 1;
							break;
						}
					}
					if (!flag)
					{
						Console.Write(string.Concat(new string[]
						{
							"source token:",
							originalTokens.get(j).ToString(),
							"(",
							originalTokens.get(j).lineNo.ToString(),
							",",
							originalTokens.get(j).columnNo.ToString(),
							")"
						}));
						Console.Write(string.Concat(new string[]
						{
							", target token:",
							this.tokenList[num - 1].ToString(),
							"(",
							this.tokenList[num - 1].lineNo.ToString(),
							",",
							this.tokenList[num - 1].columnNo.ToString(),
							")"
						}));
						break;
					}
				}
			}
			return flag;
		}

		// Token: 0x06001130 RID: 4400 RVA: 0x00314010 File Offset: 0x00312210
		public virtual void addSymbol(string symbol)
		{
			TSourceToken st = new TSourceToken(symbol);
			this.addToken(st);
		}

		// Token: 0x06001131 RID: 4401 RVA: 0x0031402C File Offset: 0x0031222C
		public virtual void addSymbol(string symbol, int tokenCode)
		{
			this.addToken(new TSourceToken(symbol)
			{
				tokencode = tokenCode
			});
		}

		// Token: 0x06001132 RID: 4402 RVA: 0x0031404E File Offset: 0x0031224E
		public virtual void addToken(TSourceToken st)
		{
			if (this.tokenList.Count != 0 || (st.tokencode != 259 && st.tokencode != 260))
			{
				this.tokenList.Add(st);
			}
		}

		// Token: 0x06001133 RID: 4403 RVA: 0x00314084 File Offset: 0x00312284
		public virtual void addSpace(int count)
		{
			this.addToken(new TSourceToken("".PadLeft(count))
			{
				tokencode = 259
			});
		}

		// Token: 0x06001134 RID: 4404 RVA: 0x003140B4 File Offset: 0x003122B4
		public virtual void addNewline()
		{
			this.addToken(new TSourceToken(TBaseType.linebreak)
			{
				tokencode = 260
			});
		}

		// Token: 0x06001135 RID: 4405 RVA: 0x003140E0 File Offset: 0x003122E0
		public virtual void addSemicolon()
		{
			this.addToken(new TSourceToken(";")
			{
				tokentype = ETokenType.ttsemicolon
			});
		}

		// Token: 0x06001136 RID: 4406 RVA: 0x00314108 File Offset: 0x00312308
		public virtual void addIdentifier(string text)
		{
			this.addToken(new TSourceToken(text)
			{
				tokencode = 264,
				tokentype = ETokenType.ttidentifier
			});
		}

		// Token: 0x06001137 RID: 4407 RVA: 0x00314138 File Offset: 0x00312338
		public virtual void acceptOracleHint(string text)
		{
			this.addToken(new TSourceToken(text)
			{
				tokencode = 257,
				tokentype = ETokenType.ttbracketedcomment
			});
		}

		// Token: 0x06001138 RID: 4408 RVA: 0x00314168 File Offset: 0x00312368
		public virtual void addKeyword(string text)
		{
			TSourceToken tsourceToken = new TSourceToken(text);
			tsourceToken.tokentype = ETokenType.ttkeyword;
			if (this.tokenList.Count > 0 && this.tokenList[this.tokenList.Count - 1].tokencode != 259)
			{
				this.addSpace(1);
			}
			this.addToken(tsourceToken);
			this.addSpace(1);
		}

		// Token: 0x06001139 RID: 4409 RVA: 0x003141CC File Offset: 0x003123CC
		public virtual void addOperator(string text, ETokenType tokenType)
		{
			TSourceToken tsourceToken = new TSourceToken(text);
			tsourceToken.tokentype = tokenType;
			this.addSpace(1);
			this.addToken(tsourceToken);
			this.addSpace(1);
		}

		// Token: 0x0600113A RID: 4410 RVA: 0x003141FC File Offset: 0x003123FC
		public virtual void addLiteral(string text)
		{
			TSourceToken st = new TSourceToken(text);
			this.addToken(st);
		}

		// Token: 0x0400139C RID: 5020
		private IList<TSourceToken> tokenList = new List<TSourceToken>();
	}
}
